package com.pkk.cloud.support.gateway.auth;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.pkk.components.rpc.response.util.ResponseUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.MediaType;

/**
 * @description: token过滤器
 * @author: peikunkun
 * @create: 2020-03-17 13:07
 **/
@Slf4j
//@Component
public class AuthTokenFilter extends ZuulFilter {

  /**
   * @Description: 网关前置
   * @return: java.lang.String
   * @Author: peikunkun
   * @Date: 2020/3/17 0017 下午 2:10
   */
  @Override
  public String filterType() {
    return FilterConstants.PRE_TYPE;
  }

  @Override
  public int filterOrder() {
    return 0;
  }

  @Override
  public boolean shouldFilter() {
    //暂时不生效,底层实现之后在返回true
    return false;
  }

  @Override
  public Object run() throws ZuulException {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    HttpServletResponse response = ctx.getResponse();
    // 设置允许跨域访问Access-Control-Allow-Origin设置的为当前dinner工程的IP+端口
    response.setHeader("Access-Control-Allow-Headers", "Authentication");
    response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
    response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
    //是否支持cookie跨域
    response.setHeader("Access-Control-Allow-Credentials", "true");

    //需要字段进行解密
    // TODO: 2020/3/17 0017 暂时不对参数进行加解密
    //this.setRequestQueryParams();

    log.info("send {} request to{}", request.getMethod(),
        request.getRequestURL().toString() + "--" + request.getContentType());
    if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
      return null;
    }
    //获取请求路径
    String requestURI = request.getRequestURI();
    // TODO: 2020/3/17 0017  判断是否包含在不拦截路径中
    //判断是否包含在不拦截路径中
    //if (isExcludePath(excludPath, requestURI)) {
    //  return null;
    //}
    String token = request.getHeader("TOKEN");
    if (StringUtils.isBlank(token)) {
      ctx.setSendZuulResponse(false);
      ctx.setResponseStatusCode(200);
      ctx.setResponseBody(JSONObject.toJSONString(ResponseUtil.error("token is not blank")));
      ctx.getResponse().setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
      return ctx;
    }

    // TODO: 2020/3/17 0017 验证token进行鉴权
    //LoginInfo loginInfo = new LoginInfo(token, "sds");
    //ReturnJson result = this.provideUserService.checkOutLogin(loginInfo);
    //log.info("鉴权中心鉴定结果是：{}", result.getMsg());
    //授权中心 回复不是成功是返回相应的错误信息
    //boolean handResult = !(ReturnJson.success().getCode().equals(result.getCode()));
    boolean handResult = false;
    if (handResult) {
      ctx.setSendZuulResponse(false);
      ctx.setResponseStatusCode(200);
      ctx.setResponseBody(JSONObject.toJSONString(ResponseUtil.success()));
      ctx.getResponse().setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
      return ctx;
    }
    ctx.addZuulRequestHeader("TOKEN", token);
    return null;
  }
}
